<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/my_library.php");
//Includes
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtpagecontrol.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Veh_Workorder_Create extends Page
{
       public $hfInitTrouble = null;
       public $hfTrouble = null;
       public $btnTrouble = null;
       public $edTrouble = null;
       public $Label3 = null;
       public $JTDivInfo = null;
       public $JTGroupBox5 = null;
       public $Label13 = null;
       public $imgInfo = null;
       public $JSValidate = null;
       public $hfError = null;
       public $lblod = null;
       public $lblordernum = null;
       public $Label12 = null;
       public $hfStartOdometer = null;
       public $hfTrackBy = null;
       public $edKmRun = null;
       public $Label2 = null;
       public $hfType = null;
       public $Label1 = null;
       public $edTax = null;
       public $lblTax = null;
       public $lbComputedlTax = null;
       public $edComputedTax = null;
       public $btnMisc = null;
       public $JTFrameMisc = null;
       public $edMisc = null;
       public $JSAJAX = null;
       public $hiddenWO = null;
       public $btnParts = null;
       public $JTFrameParts = null;
       public $JTPageControl1 = null;
       public $btnAddLabor = null;
       public $JTFrameLabor = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $JTJavaScript9 = null;
       public $JTJavaScript8 = null;
       public $JTJavaScript7 = null;
       public $btnAddDescription = null;
       public $btnDelDescription = null;
       public $edDescription = null;
       public $listDescription = null;
       public $JTDivDescription = null;
       public $JTGroupBox4 = null;
       public $btnShowDescription = null;
       public $JTJavaScript6 = null;
       public $JTJavaScript5 = null;
       public $JTJavaScript4 = null;
       public $btnAddServiced = null;
       public $edServiced = null;
       public $btnDelServiced = null;
       public $listServiced = null;
       public $JTDivServiced = null;
       public $JTGroupBox3 = null;
       public $btnShowServiced = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $compid = null;
       public $JTDivAccount = null;
       public $JTGroupBox2 = null;
       public $btnAddAccount = null;
       public $btnDelAccount = null;
       public $edAccount = null;
       public $listAccount = null;
       public $btnShowAccount = null;
       public $UploadPDF = null;
       public $memoRemarks = null;
       public $JTSiteTheme1 = null;
       public $chkLabor = null;
       public $chkMisc = null;
       public $chkParts = null;
       public $JTGroupBox1 = null;
       public $lblTotal = null;
       public $edTotal = null;
       public $lblDiscount = null;
       public $edDiscount = null;
       public $edParts = null;
       public $lblParts = null;
       public $lblMisc = null;
       public $edLabor = null;
       public $lblLabor = null;
       public $imgBack = null;
       public $Image1 = null;
       public $edOdometer = null;
       public $lblOdometer = null;
       public $jtStatus = null;
       public $chkComplete = null;
       public $chkRecall = null;
       public $chkWarr = null;
       public $lblMaint1 = null;
       public $memoFullDescription = null;
       public $hiddenLine = null;
       public $lblFullDescription = null;
       public $cmbDescription = null;
       public $lblDescription = null;
       public $cmbAcct = null;
       public $lblAcct = null;
       public $cmbServiced = null;
       public $lblServiced = null;
       public $cmbDriver = null;
       public $lblDriver = null;
       public $edOrderNo = null;
       public $lblOrderNo = null;
       public $dpCompletionDate = null;
       public $lblCompletionDate = null;
       public $dpCreationDate = null;
       public $lblCreationDate = null;
       function btnTroubleJSClick($sender, $params)
       {

       ?>
       var win_height = 550;
       var win_width = 610;
       var height = window.outerHeight;
       var width = window.outerWidth;
       var top = Math.round((height-win_height)/2);
       var left = Math.round((width-win_width)/2);

       veh_id = document.getElementById("hiddenLine").value;

       window.open("select_trouble.php?veh_id="+veh_id, 'Select_Trouble', 'location=no,height='+win_height+',width='+win_width+',top='+top+',left='+left);
       <?php

       }

       function imgInfoJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivInfo').Show();
       <?php

       }

       function edOdometerJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edOdometer','lblod');
       <?php

       }

       function edOrderNoJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edOrderNo','lblordernum');
       <?php

       }

       function edKmRunJSKeyUp($sender, $params)
       {

       ?>
       var start_odometer = document.getElementById('hfStartOdometer');
       var edit_odometer = document.getElementById('edOdometer');
       var km_run = document.getElementById('edKmRun');

       edit_odometer.value = Number(km_run.value) + Number(start_odometer.value);
       <?php

       }

       function edOdometerJSKeyUp($sender, $params)
       {

       ?>
       var start_odometer = document.getElementById('hfStartOdometer');
       var edit_odometer = document.getElementById('edOdometer');
       var km_run = document.getElementById('edKmRun');

       km_run.value = Number(edit_odometer.value) - Number(start_odometer.value);
       <?php

       }

       function btnMiscJSClick($sender, $params)
       {

       ?>
       var wo=document.getElementById('hiddenWO').value;
       var veh=document.getElementById('hiddenLine').value;

       window.open("veh_misc_create.php?veh="+veh+"&WO="+wo,"Window1","menubar=no,width=664,height=336,left=350,top=250,toolbar=no");
       <?php

       }


       function chkPartsJSChange($sender, $params)
       {

       ?>
       this.setWODetails();
       <?php

       }

       function chkLaborJSChange($sender, $params)
       {

       ?>
       this.setWODetails();
       <?php

       }



       function imgBackClick($sender, $params)
       {
       $this->deleteCancel();
       redirect('veh_workorder_list.php?veh='.$this->hiddenLine->getValue());
       }

       function btnPartsJSClick($sender, $params)
       {

       ?>
       var wo=document.getElementById('hiddenWO').value;
       var veh=document.getElementById('hiddenLine').value;

       window.open("veh_parts_create.php?veh="+veh+"&WO="+wo,"Window1","menubar=no,width=664,height=336,left=350,top=250,toolbar=no");
       <?php

       }

       function btnAddLaborJSClick($sender, $params)
       {

       ?>
       var wo=document.getElementById('hiddenWO').value;
       var veh=document.getElementById('hiddenLine').value;

       window.open("veh_laborline_create.php?veh="+veh+"&WO="+wo,"Window1","menubar=no,width=664,height=336,left=350,top=250,toolbar=no");
       <?php

       }

       function btnDelDescriptionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var DescIndex = document.getElementById('listDescription').selectedIndex;
       var DescValue = document.getElementById('listDescription')[DescIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxDesc3(DescValue,comp);
       <?php

       }

       function btnAddDescriptionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var adddesc = document.getElementById('edDescription').value;
       var compider = document.getElementById('compid').value;
       AjaxDesc2(adddesc,compider);
       document.Veh_Workorder_Create.edDescription.value = "";
       <?php

       }

       function btnShowDescriptionJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivDescription').Show();
       document.Veh_Workorder_Create.edDescription.value = "";
       <?php

       }

       function btnAddServicedJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addserviced = document.getElementById('edServiced').value;
       var compider = document.getElementById('compid').value;
       AjaxServiced2(addserviced,compider);
       document.Veh_Workorder_Create.edServiced.value = "";
       <?php

       }

       function btnDelServicedJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var ServicedIndex = document.getElementById('listServiced').selectedIndex;
       var ServicedValue = document.getElementById('listServiced')[ServicedIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxServiced3(ServicedValue,comp);
       <?php

       }


       function btnShowServicedJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivServiced').Show();
       document.Veh_Workorder_Create.edServiced.value = "";
       <?php

       }


       function btnDelAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var AccountIndex = document.getElementById('listAccount').selectedIndex;
       var AccountValue = document.getElementById('listAccount')[AccountIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxAccount3(AccountValue,comp);
       <?php

       }


       function btnAddAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addaccount = document.getElementById('edAccount').value;
       var compider = document.getElementById('compid').value;
       AjaxAccount2(addaccount,compider);
       document.Veh_Workorder_Create.edAccount.value = "";
       <?php

       }

       function btnShowAccountJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivAccount').Show();
       document.Veh_Workorder_Create.edAccount.value = "";
       <?php

       }

       function chkMiscJSChange($sender, $params)
       {

       ?>
       this.setWODetails();
       <?php

       }





       function edComputedTaxJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       //document.Veh_Workorder_Create.edTotal.value = Number(document.Veh_Workorder_Create.edMisc.value) +
       //Number(document.Veh_Workorder_Create.edComputedTax.value) -
       //Number(document.Veh_Workorder_Create.edDiscount.value);
       this.setWODetails();
       <?php

       }

       function edDiscountJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       //document.Veh_Workorder_Create.edTotal.value = Number(document.Veh_Workorder_Create.edMisc.value) +
       //Number(document.Veh_Workorder_Create.edComputedTax.value) -
       //Number(document.Veh_Workorder_Create.edDiscount.value);
       this.setWODetails();
       <?php

       }

       function edMiscJSKeyUp($sender, $params)
       {

       ?>
       //Number(document.Veh_Workorder_Create.edComputedTax.value * Number(document.Veh_Workorder_Create.edTax.value/100)) +
       //document.Veh_Workorder_Create.edComputedTax.value = Number(document.Veh_Workorder_Create.edMisc.value * Number(document.Veh_Workorder_Create.edTax.value/100));

       //document.Veh_Workorder_Create.edTotal.value = Number(document.Veh_Workorder_Create.edMisc.value) +
       //Number(document.Veh_Workorder_Create.edComputedTax.value) -
       //Number(document.Veh_Workorder_Create.edDiscount.value);
       this.setWODetails();
       <?php

       }

       function generate_workorder()
       {
        $company_id = $_SESSION['company_id'];
        $result_wo = mysql_query("SELECT * FROM company_workorders_tbl WHERE company_id='".$company_id."'")
        or die("generate workorder ".mysql_error());
        $row_wo = mysql_fetch_array($result_wo);

        $auto_num = $row_wo['auto_numbering'];

        if($auto_num==1)
        {
        $this->edOrderNo->ReadOnly = true;
        $prefix = $row_wo['prefix'];
        $new_num = $row_wo['last_order_no'] + 1;
        $new_wo = $prefix.'-'.$new_num;
        }
        if($auto_num==0)
        {
        $this->edOrderNo->ReadOnly = false;
        $new_wo = $this->edOrderNo->Text;
        }
        return $new_wo;
       }
       function generate_last_workorder()
       {
        $company_id = $_SESSION['company_id'];
        $result_wo = mysql_query("SELECT * FROM company_workorders_tbl WHERE company_id='".$company_id."'")
        or die("generate workorder ".mysql_error());
        $row_wo = mysql_fetch_array($result_wo);

        $auto_num = $row_wo['auto_numbering'];

        if($auto_num==1)
        {
        $this->edOrderNo->ReadOnly = true;
        $prefix = $row_wo['prefix'];
        $last_num = $row_wo['last_order_no']+1;
        $this->edOrderNo->Text = $prefix.'-'.$last_num;
        }
        if($auto_num==0)
        {
        $this->edOrderNo->ReadOnly = false;
        $this->edOrderNo->Text = '';
        }
       }
       function lblAddLaborlineJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       window.open("veh_laborline_create.php,"Window1","menubar=no,width=770,height=470,toolbar=no,align=center");
       <?php

       }

       function JTPlatinumVehicleLaborlineShow($sender, $params)
       {
       $veh_id = $this->hiddenLine->getValue();
       $query1 = "select * from veh_laborline_tbl where company_id='".$_SESSION['company_id']."' and vehicle_id='".$veh_id."'";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();
       while($row = mysql_fetch_array($result))
       {

       $labor_unit = "<a href=veh_laborline_edit.php?veh=".$veh_id."&laborline=".$row['labor_id'].">".$row['labor_unit']."</a>";


       $arr[] = array(
                        'Units' => $labor_unit,
                        'Cost/Unit' => $row['cost'],
                        'Description' => $row['description']
                        );
       }

       $this->JTPlatinumVehicleLaborline->setCellData($arr);


       }




       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr This field is required &darr';
             $this->hfError->setValue('1');
          }
       }


       function Veh_Workorder_CreateCreate($sender, $params)
       {

       $this->hfError->setValue('0');

       $this->lblod->Caption = '';
       $this->lblordernum->Caption = '';

       $this->compid->Value = $_SESSION['company_id'];
       $veh_id = $_GET['veh'];
       $this->hfType->setValue('create');
       if(isset($veh_id))
       {
       $this->hiddenLine->setValue($veh_id);
       $this->generate_last_workorder();

       $query_insert_wo = "INSERT INTO veh_workorder_tbl(company_id,vehicle_id) VALUES ('".$_SESSION['company_id']."','".$this->hiddenLine->getValue()."')";
       mysql_query($query_insert_wo) or die("INSERT WO ".mysql_error());

       $query_last_id = "SELECT last_insert_id(workorder_id) as last_id FROM `veh_workorder_tbl` ORDER BY workorder_id DESC LIMIT 0,1";
       $result_last_id = mysql_query($query_last_id) or die("LAST ID ".mysql_error());
       $row_last_id = mysql_fetch_array($result_last_id);
       $this->hiddenWO->setValue($row_last_id['last_id']);
       //$this->lblAddLaborline->setLink('veh_laborline_create.php?veh='.$this->hiddenLine->getValue());
       $this->JTFrameLabor->setURL('veh_laborline_list.php?veh='.$this->hiddenLine->getValue().'&WO='.$this->hiddenWO->getValue().'&type='.$this->hfType->getValue());
       $this->JTFrameParts->setURL('veh_parts_list.php?veh='.$this->hiddenLine->getValue().'&WO='.$this->hiddenWO->getValue().'&type='.$this->hfType->getValue());
       $this->JTFrameMisc->setURL('veh_misc_list.php?veh='.$this->hiddenLine->getValue().'&WO='.$this->hiddenWO->getValue().'&type='.$this->hfType->getValue());

       $this->cmbDriver->Clear();
       $this->cmbDriver->AddItem('--Select--',null,null);
        $this->cmbDriver->setItemIndex('--Select--');
       $result_driver = mysql_query("select * from employee_tbl where company_id='".$_SESSION['company_id']."' and veh_op='1'");

       while($row_driver = mysql_fetch_array($result_driver))
       {
            $this->cmbDriver->AddItem($row_driver['lname'].','.$row_driver['fname'], null, $row_driver['autoid']);
       }

       $this->cmbAcct->Clear();
       $this->cmbAcct->AddItem('--Select--',null,null);
        $this->cmbAcct->setItemIndex('--Select--');
        $result_account = mysql_query("select * from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

        while($row_account = mysql_fetch_array($result_account))
        {
            $this->cmbAcct->AddItem($row_account['value'], null, $row_account['value']);
        }

        //list account
        $result_a = mysql_query("select value from lookup_expense_tbl where company_id = '".$_SESSION['company_id']."' AND category='accounts'");

        while($row_a = mysql_fetch_array($result_a))
        {
            $this->listAccount->AddItem($row_a['value'], null, $row_a['value']);
        }

        $this->cmbDescription->Clear();
        $this->cmbDescription->AddItem('--Select--',null,null);
        $this->cmbDescription->setItemIndex('--Select--');
        $result_desc = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='descriptions'");

        while($row_desc = mysql_fetch_array($result_desc))
        {
            $this->cmbDescription->AddItem($row_desc['value'], null, $row_desc['value']);
        }

        //list description
        $result_d = mysql_query("select value from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='descriptions'");

        while($row_d = mysql_fetch_array($result_d))
        {
            $this->listDescription->AddItem($row_d['value'], null, $row_d['value']);
        }

        $this->cmbServiced->Clear();
        $this->cmbServiced->AddItem('--Select--',null,null);
        $this->cmbServiced->setItemIndex('--Select--');
        $result_ServiceProvider = mysql_query("select * from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='serviced by'");

        while($row_ServiceProvider = mysql_fetch_array($result_ServiceProvider))
        {
            $this->cmbServiced->AddItem($row_ServiceProvider['value'], null, $row_ServiceProvider['value']);
        }
        //$this->cmbServiced->AddItem('None',null,null);

        //list serviced by
        $result_s = mysql_query("select value from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND category='serviced by'");

        while($row_s = mysql_fetch_array($result_s))
        {
            $this->listServiced->AddItem($row_s['value'], null, $row_s['value']);
        }

       $this->edKmRun->Text="";
       $this->dpCreationDate->Text= date('Y-m-d');
       $this->dpCompletionDate->Text= date('Y-m-d');
       $this->edOdometer->Text="";
       $this->memoFullDescription->Text="&nbsp;";
       $this->memoRemarks->Text="&nbsp;";
       $this->edLabor->Text="";
       $this->edMisc->Text="";
       $this->edParts->Text="";
       $this->edDiscount->Text="";
       $this->edTax->Text="";
       $this->edComputedTax->Text="";
       $this->edTotal->Text ="";

       $this->edTax->Hidden=true;
       $this->lblTax->Hidden=true;
       $this->chkLabor->Hidden=true;
       $this->chkMisc->Hidden=true;
       $this->chkParts->Hidden=true;

       $result_odometer = mysql_query("select * from vehicle_tbl where vehicle_id='".$this->hiddenLine->getValue()."'");
       $row_od = mysql_fetch_array($result_odometer);



       $label_od = $row_od['track_by'];
       $stard_odometer = $row_od['tracking_value'];
       $this->hfTrackBy->setValue($label_od);
       $this->hfStartOdometer->setValue($stard_odometer);
       if ($label_od=='distance')
        {
        $this->lblOdometer->Caption = '*Odometer:';
        $this->Label2->Visible = true;
        $this->edKmRun->Visible = true;

        $this->edOdometer->Text=$row_od['curr_tracking_value'];
        }
       if ($label_od=='hours')
        {
        $this->lblOdometer->Caption = '*Hours:';
        $this->Label2->Visible = false;
        $this->edKmRun->Visible = false;

        $this->edOdometer->Text=$row_od['curr_hours'];
        }


       $result_order = mysql_query("select * from company_workorders_tbl where company_id='".$_SESSION['company_id']."'");
       $row_order = mysql_fetch_array($result_order);
       //$this->edOrderNo->Text=$row_order['prefix'].$row_order['last_order_no'];

       $result_billing =mysql_query("select * from company_billing_tbl where company_id='".$_SESSION['company_id']."'");
       $row_billing = mysql_fetch_array($result_billing);
       $label_billing = $row_billing['automatically_calculate'];
       if ($label_billing=='1')
       {
       $this->edTax->Hidden=false;
       $this->lblTax->Hidden=false;
       $this->edTax->Text=$row_billing['percent'];
       $this->chkLabor->Hidden=false;
       $this->chkLabor->Caption="";
       $this->chkMisc->Hidden=false;
       $this->chkMisc->Caption="";
       $this->chkParts->Hidden=false;
       $this->chkParts->Caption="";
       }
       else
       {
       $this->edTax->Hidden=true;
       $this->lblTax->Hidden=true;
       $this->chkLabor->Hidden=true;
       $this->chkMisc->Hidden=true;
       $this->chkParts->Hidden=true;
       }

       $label_billing = $row_billing['labor'];
       if($label_billing=='1')
       {
       $this->chkLabor->Checked=true;
       }
       else
       {
       $this->chkLabor->Checked=false;
       }

       $label_billing = $row_billing['labor'];
       if($label_billing=='1')
       {
       $this->chkLabor->Checked=true;
       }
       else
       {
       $this->chkLabor->Checked=false;
       }

       $label_billing = $row_billing['miscellaneous'];
       if($label_billing=='1')
       {
       $this->chkMisc->Checked=true;
       }
       else
       {
       $this->chkMisc->Checked=false;
       }

       $label_billing = $row_billing['parts'];
       if($label_billing=='1')
       {
       $this->chkParts->Checked=true;
       }
       else
       {
       $this->chkParts->Checked=false;
       }

       //$this->imgBack->setLink('veh_workorder_list.php?veh='.$this->hiddenLine->getValue());

       //$misc_check = $this->chkMisc->Checked;
       //if($misc_check==true)
       //$this->edMisc->Enabled = true;
       //if($misc_check==false)
       //$this->edMisc->Enabled = false;
       }
       }

       function btnCancelClick($sender, $params)
       {
       $this->deleteCancel();
       redirect('veh_workorder_list.php?veh='.$this->hiddenLine->getValue());
       }

       function deleteCancel()
       {
        $query_delete_wo = "DELETE FROM veh_workorder_tbl WHERE workorder_id='".$this->hiddenWO->getValue()."'";
        mysql_query($query_delete_wo) or die("DELETE WO ".mysql_error());

        $query_delete_laborline = "DELETE FROM veh_laborline_tbl WHERE workorder='".$this->hiddenWO->getValue()."'";
        mysql_query($query_delete_laborline) or die("DELETE LABORLINE ".mysql_error());

        $query_sel_parts = "SELECT parts_id,quantity,part_num FROM veh_parts_tbl WHERE workorder='".$this->hiddenWO->getValue()."'";
        $result_sel_parts = mysql_query($query_sel_parts) or die("SELECT PARTS ".mysql_error());
        while($row_sel_parts = mysql_fetch_array($result_sel_parts))
        {
         $part_id = $row_sel_parts['parts_id'];
         $qty_delete = $row_sel_parts['quantity'];
         $stock_id = $row_sel_parts['part_num'];

         $query_inv = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
         $result_inv = mysql_query($query_inv) or die('INVENTORY - QUANTITY '.mysql_error());
         $row_inv = mysql_fetch_array($result_inv);
         $curr_qty = $row_inv['qty_available'];

         $new_qty = $curr_qty + $qty_delete;
         $query_up_inv = "UPDATE invstocks_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
         mysql_query($query_up_inv) or die("UPDATE INV ".mysql_error());

         $query_select_inv_stat = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
         $result_select_inv_stat = mysql_query($query_select_inv_stat) or die("SELECT INV STAT ".mysql_error());
         $count_inv_stat = mysql_num_rows($result_select_inv_stat);

         if($count_inv_stat>0)
         {
          $query_up_inv = "UPDATE invstocks_status_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
          mysql_query($query_up_inv) or die("UPDATE INV STATUS ".mysql_error());
         }

         $query_delete_parts = "DELETE FROM veh_parts_tbl WHERE parts_id ='".$part_id."'";
         mysql_query($query_delete_parts) or die("DELETE PARTS ".mysql_error());
        }
       }

       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->edOrderNo->Text, 'lblordernum');
       $this->validateRequired($this->edOdometer->Text, 'lblod');
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {



       $my_library = new my_library();


       if ($this->chkLabor->Checked==true)
       {
       $chkLabor ='1';
       }
       else
       {
       $chkLabor='0';
       }

       if ($this->chkMisc->Checked==true)
       {
       $chkMisc ='1';
       }
       else
       {
       $chkMisc='0';
       }

       if ($this->chkParts->Checked==true)
       {
       $chkParts ='1';
       }
       else
       {
       $chkParts='0';
       }


       if ($this->chkWarr->Checked==true)
       {
       $chkWarr ='1';
       }
       else
       {
       $chkWarr='0';
       }


       if ($this->chkRecall->Checked==true)
       {
       $chkRecall ='1';
       }
       else
       {
       $chkRecall='0';
       }


       if ($this->chkComplete->Checked==true)
       {
       $chkComplete ='1';
       }
       else
       {
       $chkComplete='0';
       }

       $vehicle_id = $this->hiddenLine->getValue();
       $new_wo = $this->generate_workorder();


       //$my_library->updateCurrentTrackingValue($vehicle_id, $this->edOdometer->Text);


       $result_wo_id = mysql_query("SELECT MAX(workorder_id) as max FROM veh_workorder_tbl") or die("SELECT WO ID - ".mysql_error());
       $row_wo_id = mysql_fetch_array($result_wo_id);
       $wo_id = $row_wo_id['max']+1;


       /*mysql_query("INSERT INTO veh_workorder_tbl(vehicle_id,
       creation_date,
       completion_date,
       order_num,
       odometer,
       driver,
       service_provider,
       account,
       description,
       full_desc,
       maint_custom1,
       labor,
       misc,
       parts,
       discount,
       tax,
       computed_tax,
       total,
       ch_labor,
       ch_misc,
       ch_parts,
       warr,
       backjob,
       complete,
       attachment)

       VALUES
       ('".$this->hiddenLine->getValue()."',
       '".$this->dpCreationDate->Text."',
       '".$this->dpCompletionDate->Text."',
       '".$new_wo."',
       '".$this->edOdometer->Text."',
       '".$this->cmbDriver->getItemIndex()."',
       '".$this->cmbServiced->getItemIndex()."',
       '".$this->cmbAcct->getItemIndex()."',
       '".$this->cmbDescription->getItemIndex()."',
       '".$this->memoFullDescription->Text."',
       '".$this->memoRemarks->Text."',
       '".$this->edLabor->Text."',
       '".$this->edMisc->Text."',
       '".$this->edParts->Text."',
       '".$this->edDiscount->Text."',
       '".$this->edTax->Text."',
       '".$this->edComputedTax->Text."',
       '".$this->edTotal->Text."',
       '".$chkLabor."',
       '".$chkMisc."',
       '".$chkParts."',
       '".$chkWarr."',
       '".$chkRecall."',
       '".$chkComplete."',
       '".$this->UploadPDF->FileName."')") or die("INSERT WORKORDER - ".mysql_error());
       */
       $service = $this->cmbServiced->getItemIndex();
       $result_service = mysql_query("select value from lookup_workorder_tbl where company_id = '".$_SESSION['company_id']."' AND id='".$service."'");
       $row_service = mysql_fetch_array($result_service);

       $track_by = $this->hfTrackBy->getValue();
       if($track_by=='distance')
       {
       $track_save = 'odometer';
       $label = 'Km';
       }
       if($track_by=='hours')
       {
       $track_save = 'hours';
       $label = 'Hrs';
       }

       $query_update = "UPDATE veh_workorder_tbl SET
       trouble_ids='".$this->hfTrouble->getValue()."',
       creation_date='".$this->dpCreationDate->Text."',
       completion_date='".$this->dpCompletionDate->Text."',
       driver = '".$this->cmbDriver->getItemIndex()."',
       order_num='".$this->edOrderNo->Text."',
       odometer='".$this->edOdometer->Text."',
       track_by='".$this->hfTrackBy->getValue()."',
       service_provider='".$this->cmbServiced->getItemIndex()."',
       service_id='".$this->cmbServiced->getItemindex()."',
       account='".$this->cmbAcct->getItemindex()."',
       description='".$this->cmbDescription->getItemIndex()."',
       full_desc='".$this->memoFullDescription->Text."',
       maint_custom1='".$this->memoRemarks->Text."',
       labor='".$this->edLabor->Text."',
       misc='".$this->edMisc->Text."',
       parts='".$this->edParts->Text."',
       discount='".$this->edDiscount->Text."',
       tax='".$this->edTax->Text."',
       computed_tax='".$this->edComputedTax->Text."',
       total='".$this->edTotal->Text."',
       ch_labor='".$chkLabor."',
       ch_misc='".$chkMisc."',
       ch_parts='".$chParts."',
       warr = '".$chkWarr."',
       backjob='".$chkRecall."',
       complete='".$chkComplete."',
       attachment='".$this->UploadPDF->FileName."',
       attachment2='pdf".$wo_id.'.'.$this->UploadPDF->FileExt."'
       WHERE vehicle_id='".$this->hiddenLine->getValue()."'and workorder_id ='".$this->hiddenWO->getValue()."'";
       mysql_query($query_update) or die(mysql_error());

       //FOR TROUBLE USED FLAG
       $trouble_ids = explode(',',$this->hfTrouble->getValue());
       foreach($trouble_ids as $trouble_id)
       {
        $query_up_trouble = "UPDATE veh_trouble_tbl SET flag_used='1' WHERE
        autoid='".$trouble_id."'";
        mysql_query($query_up_trouble) or die("UPDATE TROUBLE USED FLAG ".mysql_error());
       }

       $this->UploadPDF->moveUploadedFile("photo/pdf/pdf".$wo_id.".".$this->UploadPDF->FileExt);

       $query_track_val = "SELECT * FROM vehicle_tbl WHERE vehicle_id='".$this->hiddenLine->getValue()."'";
       $result_track_val = mysql_query($query_track_val) or die("SELECT CURR TRACKING VALUE ".mysql_error());
       $row_track_val = mysql_fetch_array($result_track_val);

       //$track_by = $this->hfTrackBy->getValue();
       $track_by = $row_track_val['track_by'];
       $edit_odometer = $this->edOdometer->Text;
       if($track_by=='distance')
       {
        if($edit_odometer>$row_track_val['curr_tracking_value'])
        {
        $query_up_curr = "UPDATE vehicle_tbl SET curr_tracking_value='".$edit_odometer."' WHERE vehicle_id='".$this->hiddenLine->getValue()."'";
        mysql_query($query_up_curr) or die("UPDATE CURRENT TRACKING VALUE ".mysql_error());
        }
       }
       if($track_by=='hours')
       {
        if($edit_odometer>$row_track_val['curr_hours'])
        {
        $query_up_curr = "UPDATE vehicle_tbl SET curr_hours='".$edit_odometer."' WHERE vehicle_id='".$this->hiddenLine->getValue()."'";
        mysql_query($query_up_curr) or die("UPDATE CURRENT TRACKING VALUE ".mysql_error());
        }
       }

       $company_id = $_SESSION['company_id'];
       $result_last_order_num = mysql_query("SELECT last_order_no as last from company_workorders_tbl WHERE
       company_id='".$company_id."'") or die("get last order ".mysql_error());
       $row_lon = mysql_fetch_array($result_last_order_num);
       //var_dump($row_lon);
       $lon = $row_lon['last'];
       $lon++;
       mysql_query("UPDATE company_workorders_tbl SET last_order_no='".$lon."' WHERE
       company_id='".$company_id."'") or die("update last company rec ".mysql_error());

       //redirect('veh_workorder_list.php?veh='.$this->hiddenLine->getValue());
       echo "<script type='text/javascript'>
       alert('Successfully added record!');
       var answer = confirm('Click OK to add another record or CANCEL to go back to list');
       if(answer)
       {
       window.location = 'veh_workorder_create.php?veh=".$this->hiddenLine->getValue()."';
       }
       else
       {
       window.location = 'veh_workorder_list.php?veh=".$this->hiddenLine->getValue()."';
       }
       </script>";
       }

       }

}

global $application;

global $Veh_Workorder_Create;

//Creates the form
$Veh_Workorder_Create=new Veh_Workorder_Create($application);

//Read from resource file
$Veh_Workorder_Create->loadResource(__FILE__);

//Shows the form
$Veh_Workorder_Create->show();

?>